package com.neu.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableRowSorter;

import com.neu.pojo.Patient;
import com.neu.pojo.Room;
import com.neu.pojo.RoomRecord;
import com.neu.pojo.Worker;
import com.neu.service.impl.PatientService;
import com.neu.service.impl.RoomRecordService;
import com.neu.service.impl.RoomService;
import com.neu.table.RoomTable;

import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.JComboBox;
import javax.swing.JButton;

public class RoomUI extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private RoomTable roomTable = new RoomTable();
	
	private RoomService roomService = RoomService.getInstance();
	private PatientService patientService = PatientService.getInstance();
	private RoomRecordService roomRecordService = RoomRecordService.getInstance();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RoomUI frame = new RoomUI(new Worker());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RoomUI(Worker worker) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 680, 440);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		table = new JTable(roomTable);//以 room Table的格式创建JTable 加入到面板中
		table.setBounds(23, 104, 620, 209);
		contentPane.add(table);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setBounds(29, 49, 93, 31);
		contentPane.add(comboBox);
		comboBox.addItem("全部");
		for(Room r:this.roomService.list()) {
			comboBox.addItem(r.getKind());
		}
		comboBox.addActionListener((e)->{
			TableRowSorter sorter = new TableRowSorter(this.roomTable);
			table.setRowSorter(sorter);
			String s=(String) comboBox.getSelectedItem();
			
			switch (s) {
	    	case "全部":
	    		break;
		    default:
		    	List<RowFilter<Object, Object>> filter=new ArrayList<>(2);
		    	filter.add(RowFilter.regexFilter(s));
		    	sorter.setRowFilter(RowFilter.andFilter(filter));
		    	break;
			}
		});
		
		JButton btnNewButton_1 = new JButton("添加房屋");
		btnNewButton_1.setBounds(471, 49, 90, 31);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener((e)->{//传w
			new AddRoomUI(worker).setVisible(true);
			setVisible(false);
		});
		
		JButton btnNewButton_1_1 = new JButton("删除房屋");
		btnNewButton_1_1.setBounds(562, 49, 90, 31);
		contentPane.add(btnNewButton_1_1);
		btnNewButton_1_1.addActionListener((e)->{
			ArrayList<Room> rooms = new ArrayList<>();
			
			for(int i=0;i<this.roomTable.getRowCount();i++) {
				if((boolean)this.roomTable.getValueAt(i, 0)) {//选中了
					if((int)this.roomTable.getValueAt(i, 4)==0){//没人才能删
						rooms.add(this.roomService.search(new Room((String)this.roomTable.getValueAt(i, 1))));
					}
				}
			}
			for(Room room:rooms) {
				System.out.println(this.roomService.delete(room));
			}
			new RoomUI(worker).setVisible(true);
			setVisible(false);
		});
		
//		JButton btnNewButton_1_2 = new JButton("查看详情");
//		btnNewButton_1_2.setBounds(198, 49, 90, 31);
//		contentPane.add(btnNewButton_1_2);
//		btnNewButton_1_2.addActionListener((e)->{//给用户查看此病房信息和目前所入住的人
//			int cnt = 0;
//			int tar = 0;
//			for(int i=0;i<this.roomTable.getRowCount();i++) {
//				if((boolean)this.roomTable.getValueAt(i, 0)) {
//					cnt++;
//					tar = i;
//				}
//			}
//			if(cnt!=1) {
//				JOptionPane.showMessageDialog(null, "请选择单个房间");
//			}else {
//				Room room = this.roomService.search(new Room((String)this.roomTable.getValueAt(tar, 1)));
//				JOptionPane.showMessageDialog(null, this.roomService);
//				
//			}
//		});
		
		JButton btnNewButton_2 = new JButton("返回");
		btnNewButton_2.setBounds(495, 355, 105, 38);
		contentPane.add(btnNewButton_2);
		btnNewButton_2.addActionListener((e)->{
			new WorkerUI(worker).setVisible(true);
			setVisible(false);
		});
		
		JButton btnNewButton_1_2_1 = new JButton("入住");
		btnNewButton_1_2_1.setBounds(289, 49, 90, 31);
		contentPane.add(btnNewButton_1_2_1);
		btnNewButton_1_2_1.addActionListener((e)->{//入住按钮
			int cnt = 0;
			int tar = 0;
			for(int i=0;i<this.roomTable.getRowCount();i++) {
				if((boolean)this.roomTable.getValueAt(i, 0)) {
					cnt++;
					tar = i;
				}
			}
			if(cnt!=1) {
				JOptionPane.showMessageDialog(null, "请选择单个房间");
			}else {//选了一个
				if((int)this.roomTable.getValueAt(tar, 4)<(int)this.roomTable.getValueAt(tar, 3)) {//房间有位置
					String id=JOptionPane.showInputDialog("请输入患者id:");
					Patient p = this.patientService.search(new Patient(id));
					if(p == null) {
						JOptionPane.showMessageDialog(null, "患者不存在");
					}else {
						if(this.roomRecordService.isin(p)) {
							JOptionPane.showMessageDialog(null, "患者已入住");
						}else {
							RoomRecord rr = new RoomRecord(this.roomService.search(new Room((String)this.roomTable.getValueAt(tar, 1))), p,new Date().toString(), null);
							System.out.println(this.roomRecordService.add(rr));
							System.out.println(this.roomService.livein(this.roomService.search(new Room((String)this.roomTable.getValueAt(tar, 1)))));
							JOptionPane.showMessageDialog(null, "入住成功");
							new RoomUI(worker).setVisible(true);
							setVisible(false);
						}
					}
				}else {
					JOptionPane.showMessageDialog(null, "房间已经满");
				}
			}
		});
		
		//窗口居中
		setLocationRelativeTo(null);
		
		//加滚动条
		JScrollPane jp = new JScrollPane(table);
		jp.setBounds(29, 101, 614, 229);
		contentPane.add(jp);
		
		JButton btnNewButton = new JButton("搬出");
		btnNewButton.setBounds(380, 49, 90, 31);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener((e)->{
			int cnt = 0;
			int tar = 0;
			for(int i=0;i<this.roomTable.getRowCount();i++) {
				if((boolean)this.roomTable.getValueAt(i, 0)) {
					cnt++;
					tar = i;
				}
			}
			if(cnt!=1) {
				JOptionPane.showMessageDialog(null, "请选择单个房间");
			}else {
				if((int)this.roomTable.getValueAt(tar, 4)>0) {
					String id=JOptionPane.showInputDialog("请输入患者id:");
					Patient p = this.patientService.search(new Patient(id));
					if(p == null) {
						JOptionPane.showMessageDialog(null, "患者不存在");
					}else {
						//患者存在
						if(this.roomRecordService.isin(p)) {
							RoomRecord rr = new RoomRecord(this.roomService.search(new Room((String)this.roomTable.getValueAt(tar, 1))), p,new Date().toString(), null);
							if(this.roomService.liveout(new Room((String)this.roomTable.getValueAt(tar, 1)))) {
								JOptionPane.showMessageDialog(null, "搬出成功");
								new RoomUI(worker).setVisible(true);
								setVisible(false);
							}else {
								JOptionPane.showMessageDialog(null, "搬出失败");
							}
						}else {
							JOptionPane.showMessageDialog(null, "患者未入住");
						}
					}
				}
			}
		});
	}
}
